<template>
	<view class="chat">
		<view class="chat-bg"></view>
		<scroll-view class="scroll" scroll-y show-scrollbar :scroll-into-view="toButtom" @scroll="showScroll">
			<view class="chat-info" :class="item.sendUser === 0 ? 'chat-self' : 'chat-chatter'"
				v-for="(item, index) in chatList" :key="index">
				<image :src="item.sendUser === 0 ? user.cover :  boss.cover" mode="aspectFill"
					@click="todetail(item.sendUser,boss.id)"
					style="height: 120rpx; width: 120rpx; border-radius: 50%; margin-right: 15rpx; margin-bottom: auto;">
				</image>
				<view class="chat-message">
					<text class="text-main-xll">{{ item.sendMessage }}</text>
				</view>
			</view>
			<view id="to-buttom" style="height: 12rpx;"></view>
		</scroll-view>
		<view class="zhanweifu"></view>

		<view class="input-box">
			<view class="tools" @click="clear">
				<view class="clear">
					<uni-icons type="trash" size="24"></uni-icons>
				</view>
			</view>
			<input type="text" v-model="chatData.sendMessage">
						<button class="send" @click="sendMessage">发送消息</button>
						<!-- <button :class="['animate__animated',{animate__flip:send==1 ? true : false}]" @click="sendMessage">发送消息</button> -->
		</view>
	</view>
</template>

<script>
	import {
		getDetail
	} from '@/common/mock/mock.js';
	import {
		getTime
	} from '@/utils/getTime.js';
	import {
		config
	} from '@/common/config.js';
	import listItem from '../../uni_modules/uview-ui/libs/config/props/listItem';
	export default {
		data() {
			return {
				chatData: {
					bossId: '',
					bossName: '',
					bossImage: '',
					lastMessage: '',
					lastTime: '',
					sendMessage: ''
				},
				chatList: [],
				boss: {
					id: '',
					job: '',
					price: '',
					address: '',
					cover: '',
					interviewer: '',
					label: [],
					company: ''
				},
				user: {
					id: '',
					cover: ''
				},
				toButtom: '',
				socketMessage: '',
				hasSend: false,
				send:'animate__animated animate__flip',
			}
		},
		onLoad(option) {
			// this.openSocket();
			if (option.id) {
				this.getData(option.id);
			};
			uni.setNavigationBarTitle({
				title: option.boss
			});
			uni.setNavigationBarColor({
				frontColor: "#000000",
				backgroundColor: '#00b4b3'
			});
			this.user.cover = this.$store.state.user.cover;
			this.InsertChatList(uni.getStorageSync(option.id))
			// console.log(this.chatList)
			// 获取用户名和id
			this.chatData.bossId = option.id;
			this.chatData.bossName = option.boss;
			// this.toBottom();
			// uni.sendSocketMessage("nihaoma>>")
		},
		onUnload() {
			uni.setStorageSync(this.boss.id, this.chatList);

			// 防止重复用户聊天数据
			var flag = 1;
			for (let i = 0; i < this.$store.state.Boss.length; i++) {
				console.log("this.$store.state.Boss:" + this.$store.state.Boss);
				if (this.$store.state.Boss[i].bossId === this.boss.id) {
					if (this.hasSend) {
						this.$store.state.Boss[i].lastMessage = this.chatData.lastMessage
						this.$store.state.Boss[i].lastTime = this.chatData.lastTime
					}
					flag = 0;
					break;
				}
			}
			if (flag) {
				this.$store.state.Boss.push(this.chatData);
			}
			console.log(this.$store.state.Boss);
			uni.setStorageSync("Boss",this.$store.state.Boss);
			console.log("已更新boss缓存");
			console.log(uni.getStorageSync("Boss"));
			
			this.$store.state.noneMessage = false;
			console.log(this.$store.state.noneMessage)
		},
		methods: {
			getData(id) {
				// mock
				// getDetail(id).then((res) => {
				// 	this.boss.id = res._id.$oid,
				// 		this.boss.job = res.job,
				// 		this.boss.price = res.price,
				// 		this.boss.address = res.address,
				// 		this.boss.cover = res.cover,
				// 		this.boss.interviewer = res.interviewer,
				// 		// this.context = res.context,
				// 		this.boss.label = res.label,
				// 		this.boss.company = res.company,
				// 		this.chatData.bossImage = res.cover
				// })
				// #ifdef APP-PLUS
				// uniCloud云函数
				this.$api.get_detail({
					name: 'get_detail',
					id: id
				}).then((res) => {
					console.log(res.data)
					this.boss.id = res.data._id,
						this.boss.job = res.data.job,
						this.boss.price = res.data.price,
						this.boss.address = res.data.address,
						this.boss.cover = res.data.cover,
						this.boss.interviewer = res.data.interviewer,
						// this.context = res.context,
						this.boss.label = res.data.label,
						this.boss.company = res.data.company,
						this.chatData.bossImage = res.data.cover
				})
				console.log("this.boss")
				console.log(this.boss)
				// #endif
				// #ifndef APP-PLUS
				uni.request({
					url: config.url + 'job/detail',
					data: {
						id: id
					},
					success: (res) => {
						console.log(res.data)
						this.boss.id = res.data.data.id,
							this.boss.job = res.data.data.job,
							this.boss.price = res.data.data.price,
							this.boss.address = res.data.data.address,
							this.boss.cover = res.data.data.cover,
							this.boss.interviewer = res.data.data.interviewer,
							// this.context = res.context,
							this.boss.label = res.data.data.label,
							this.boss.company = res.data.data.company,
							this.chatData.bossImage = res.data.data.cover
					}
				})
				console.log(this.boss);
				// #endif

			},
			sendMessage() {
				if (this.chatData.sendMessage) {
					// 我方接受数据
					// 还需设计
					let message = {
						to: this.chatData.bossId,
						text: this.chatData.sendMessage
					};
					console.log(message)

					// data: JSON.stringify(message)
					// console.log("发送了" + JSON.stringify(message))
					let chat_self = {
						sendUser: 0,
						sendMessage: this.chatData.sendMessage
					}
					this.chatList.push(chat_self);
					this.hasSend = true;
					// 存储最后一条数据
					this.chatData.lastMessage = this.chatData.sendMessage;
					console.log("lastMessage")
					console.log(this.chatData.lastMessage)
					this.chatData.lastTime = getTime();

					this.chatData.sendMessage = '';

					let response = this.chatData.lastMessage.toString().trim();
					console.log(response)
					if (response.indexOf('你') > -1) {
						response = response.replace('你', '我');
					} else if (response.indexOf('我') > -1) {
						response = response.replace('我', '你');
					}
					if (response.indexOf('?') > -1) {
						response = response.replace('?', '!');
					}
					if (response.indexOf('？') > -1) {
						response = response.replace('？', '!');
					}
					if (response.indexOf('吗') > -1) {
						response = response.replace('吗', '呢');
					}

					console.log("response")
					console.log(response)
					// 对方发送数据
					// var chatter = JSON.parse(response);
					var chatter = response;
					let chat_chatter = {
						sendUser: 1,
						sendMessage: chatter
					}
					this.chatList.push(chat_chatter);
					this.chatData.lastMessage = chatter;
					this.chatData.lastTime = getTime();

					console.log(this.chatData)

				} else {
					uni.showToast({
						title: '请在输入框内填写内容...',
						icon: 'none'
					});
				}
			},
			InsertChatList(List) {
				for (let i = 0; i < List.length; i++) {
					this.chatList.push(List[i])
				}
			},
			todetail(id, boss_id) {
				if (id) {
					console.log(id, boss_id)
					this.$u.route('pages/detail/detail', {
						id: boss_id
					})
				} else {
					uni.switchTab({
						url: '/pages/my/my'
					})
				}

			},
			clear() {
				this.chatList = []
				uni.showToast({
					title: '聊天记录已删除',
					icon: 'none'
				});
			}
		}
	}
</script>

<style lang="scss">
	.send {
		-webkit-animation: rotate-vert-center 1s;
		        animation: rotate-vert-center 1s;
	}
	.send:active{
		animation: none;
	}
	page {
		// background: red;
	}

	.chat-bg {
		height: 100vh;
		width: 100vw;
		background-image: linear-gradient(to top, #3f51b1 0%, #5a55ae 13%, #7b5fac 25%, #8f6aae 38%, #a86aa4 50%, #cc6b8e 62%, #f18271 75%, #f3a469 87%, #f7c978 100%);
		// background-image: linear-gradient(to top, gainsboro 0%, #ace0f9 100%);
		background-size: cover;
		box-sizing: border-box;
		position: fixed;
		z-index: 0;
	}


	.chat {
		background: gainsboro;
		width: 100%;

		.scroll {
			height: 82vh;

			.chat-info {
				display: flex;
				align-items: center;
				margin-bottom: 12rpx;

				.chat-message {

					max-width: 80%;
					padding: 15rpx;
					font-weight: bold;
					border-radius: 15rpx;
					box-shadow: rgba(60, 64, 67, 0.3) 0rpx 1rpx 2rpx 0rpx, rgba(60, 64, 67, 0.15) 0rpx 1rpx 3rpx 1rpx;
				}
			}

			.chat-chatter {
				margin-left: 15rpx;

				.chat-message {
					background-image: linear-gradient(-225deg, #fffeff 0%, #d7fffe 100%);
				}
			}

			.chat-self {
				flex-direction: row-reverse;
				margin-left: auto;

				.chat-message {
					margin: 10rpx;
					background-image: linear-gradient(-20deg, #e9defa 0%, #fbfcdb 100%);
				}

			}


		}

		.zhanweifu {
			height: 175rpx;
		}

		.input-box {
			position: fixed;
			z-index: 99;
			bottom: 0;
			width: 100%;
			height: 8%;
			padding: 10px;
			display: flex;
			flex-direction: row;
			align-items: center;
			background: white;

			.tools {
				background: red;
				height: 6vh;
				z-index: 99;
				width: 10vw;
				border-radius: 20rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				-webkit-animation: tan 1s;
				        animation: tan 1s;
			}
			.tools:active{
					animation: none;
				}
			input {
				float: left;
				width: 58vw;
				border: 1px solid black;
				border-radius: 20px;
				height: 50%;
				margin-left: 2%;
				padding: 5px 5px 5px 15px;
				background: white;
			}

			button {
				position: absolute;
				float: left;
				left: 76%;
				width: 20vw;
				height: 50%;
				margin-right: 10rpx;
				border-radius: 20rpx;
				background: $boss-base-color;
				text-align: center;
				// align-items: center;
			}
		}
	}
</style>